<template>
  <div class="sec-kill-operation">
    <operation-title
      title="秒杀"
      :icon="TitleIcon"
    />
    <div class="divider"></div>
    <div class="func">
      <p class="attr">选择风格</p>
      <ul class="common-ul">
        <li
          @click="pageConfig.custom = '1'"
          :class="pageConfig.custom == '1' ? 'active' : ''"
        >
          <img src="../../../../../../assets/app/other/goods/goods-two.png" alt="" />
        </li>
        <li
          @click="pageConfig.custom = '2'"
          :class="pageConfig.custom == '2' ? 'active' : ''"
        >
          <img src="../../../../../../assets/app/other/goods/goods-five.png" alt="" />
        </li>
        <li
          @click="pageConfig.custom = '3'"
          :class="pageConfig.custom == '3' ? 'active' : ''"
        >
          <img src="../../../../../../assets/app/other/goods/goods-eight.png" alt="" />
        </li>
        <li
          @click="pageConfig.custom = '4'"
          :class="pageConfig.custom == '4' ? 'active' : ''"
        >
          <img src="../../../../../../assets/app/other/goods/goods-seven.png" alt="" />
        </li>
      </ul>
    </div>
    <div class="divider"></div>
    <div class="func">
      <p class="attr">样式</p>
      <ul class="common-ul">
        <li
          @click="pageConfig.style = '1'"
          :class="pageConfig.style == '1' ? 'active' : ''"
        >
          默认
        </li>
        <li
          @click="pageConfig.style = '2'"
          :class="pageConfig.style == '2' ? 'active' : ''"
        >
          投影
        </li>
        <li
          @click="pageConfig.style = '3'"
          :class="pageConfig.style == '3' ? 'active' : ''"
        >
          描边
        </li>
      </ul>
      <div class="adjust-item" v-if="pageConfig.style == '3'">
        <el-color-picker v-model="pageConfig.borderColor" />
        <span>{{ pageConfig.borderColor }}</span>
      </div>
      <div class="adjust-item" v-if="pageConfig.style == '3'">
        <el-slider
          v-model="pageConfig.borderWidth"
          :min="0"
          :max="5"
          show-input
          size="small"
        />px
      </div>
    </div>
    <div class="divider"></div>
    <div class="func">
      <p class="attr">标题</p>
      <div class="adjust-item">
        <el-radio-group v-model="pageConfig.titleStyle">
          <el-radio label="text">文字</el-radio>
          <el-radio label="picture">图片</el-radio>
        </el-radio-group>
      </div>
      <div class="add-item" v-if="pageConfig.titleStyle === 'text'">
        <div class="add-item-input">
          <p>文字</p>
          <el-input
            v-model="pageConfig.title"
            maxlength="8"
            :show-word-limit="true"
          />
        </div>
        <div class="add-item-input">
          <p>颜色</p>
          <el-color-picker v-model="pageConfig.titleColor" />
          <span>{{ pageConfig.titleColor }}</span>
        </div>
      </div>
      <div class="add-item" v-else-if="pageConfig.titleStyle === 'picture'">
        <div class="add-item-input">
          <p>图片</p>
          <div class="img-placeholder">
            <div class="upload-img" v-if="pageConfig.titleImg">
              <el-image
                style="width: 100%; height: 100%"
                :src="pageConfig.titleImg"
                fit="contain"
              >
              </el-image>
              <div class="upload-img_cover">
                <Upload
                  :model-value="titleFileList"
                  file-path="actapp/"
                  :limit="999"
                  :show-file-list="false"
                  @success="titleFileSuccess"
                >
                  <el-button size="small">替换</el-button>
                </Upload>
              </div>
            </div>
            <Upload
              v-else
              v-model="pageConfig.titleImg"
              file-path="actapp/"
              list-type="picture-card"
            >
              <el-icon>
                <Plus />
              </el-icon>
            </Upload>
          </div>
        </div>
      </div>
    </div>
    <div class="divider"></div>
    <div class="func">
      <p class="attr">更多</p>
      <div class="adjust-item">
        <el-radio-group v-model="pageConfig.moreShow">
          <el-radio :label="true">显示</el-radio>
          <el-radio :label="false">隐藏</el-radio>
        </el-radio-group>
      </div>
      <div class="add-item" v-if="pageConfig.moreShow">
        <div class="add-item-input">
          <p>文字</p>
          <el-input
            v-model="pageConfig.moreText"
            maxlength="8"
            :show-word-limit="true"
          />
        </div>
        <div class="add-item-input">
          <p>颜色</p>
          <el-color-picker v-model="pageConfig.moreColor" />
          <span>{{ pageConfig.moreColor }}</span>
        </div>
        <!-- <div class="add-item-input">
          <p>地址</p>
          <span class="link-div" @click="linkDialog(pageConfig, 0)">
            <el-icon>
              <Link />
            </el-icon>
            <span v-if="pageConfig.url">{{pageConfig.urlName}}</span>
            <span v-else>请选择链接</span>
          </span>
        </div> -->
      </div>
    </div>
    <div class="divider"></div>
    <div class="func">
      <p class="attr">倒计时</p>
      <div class="adjust-item">
        <span>数字底色</span>
        <el-color-picker v-model="pageConfig.countdownBg" />
        <span>{{ pageConfig.countdownBg }}</span>
      </div>
      <div class="adjust-item">
        <span>数字颜色</span>
        <el-color-picker v-model="pageConfig.countdownColor" />
        <span>{{ pageConfig.countdownColor }}</span>
      </div>
    </div>
  </div>
  <div class="divider"></div>
  <div class="func">
    <p class="attr">选择活动</p>
    <el-cascader
      filterable
      :model-value="pageConfig.goodsSelected"
      :options="actPageAreaList"
      @change="changeGoods"
    />
  </div>

  <!--  <div class="func">-->
  <!--    <p class="attr">关联活动</p>-->
  <!--    <div class="link-div" @click="linkDialog(pageConfig, 0)">-->
  <!--      <el-icon>-->
  <!--        <Link />-->
  <!--      </el-icon>-->
  <!--      <span v-if="pageConfig.url">{{pageConfig.urlName}}</span>-->
  <!--      <span v-else>请选择链接</span>-->
  <!--    </div>-->
  <!--  </div>-->
  <div class="divider"></div>
  <div class="func">
    <p class="attr">颜色设置</p>
    <div class="adjust-item">
      <span>组件背景</span>
      <el-radio-group v-model="pageConfig.componentBgStyle">
        <el-radio label="color">背景色</el-radio>
        <el-radio label="picture">背景图</el-radio>
      </el-radio-group>
    </div>
    <div class="add-item" v-if="pageConfig.componentBgStyle === 'color'">
      <div class="add-item-input">
        <p>颜色</p>
        <el-color-picker v-model="pageConfig.componentBgColor" />
        <span>{{ pageConfig.componentBgColor }}</span>
      </div>
    </div>
    <div class="add-item" v-else-if="pageConfig.componentBgStyle === 'picture'">
      <div class="add-item-input">
        <p>图片</p>
        <div class="img-placeholder">
          <div class="upload-img" v-if="pageConfig.componentBgUrl">
            <el-image
              style="width: 100%; height: 100%"
              :src="pageConfig.componentBgUrl"
              fit="contain"
            ></el-image>
            <div class="upload-img_cover">
              <Upload
                :model-value="componentBgUrlFileList"
                file-path="actapp/"
                :limit="999"
                :show-file-list="false"
                @success="componentBgUrlFileSuccess"
              >
                <el-button size="small">替换</el-button>
              </Upload>
            </div>
          </div>
          <Upload
            v-else
            v-model="pageConfig.componentBgUrl"
            file-path="actapp/"
            list-type="picture-card"
          >
            <el-icon>
              <Plus />
            </el-icon>
          </Upload>
        </div>
      </div>
    </div>

    <div class="adjust-item">
      <span>商品背景</span>
      <el-color-picker v-model="pageConfig.goodsBgColor" />
      <span>{{ pageConfig.goodsBgColor }}</span>
    </div>
    <div class="adjust-item">
      <span style="width: 84px">商品组背景</span>
      <el-color-picker v-model="pageConfig.goodsListBgColor" />
      <span>{{ pageConfig.goodsListBgColor }}</span>
    </div>
  </div>

  <div class="func">
    <p class="attr">显示内容</p>
    <div class="adjust-item">
      <el-checkbox v-model="pageConfig.goodsNameShow">商品标题</el-checkbox>
      <el-color-picker v-model="pageConfig.goodsNameColor" />
      <span>{{ pageConfig.goodsNameColor }}</span>
    </div>
    <div class="adjust-item">
      <el-checkbox v-model="pageConfig.priceShow">商品价格</el-checkbox>
      <el-color-picker v-model="pageConfig.priceColor" />
      <span>{{ pageConfig.priceColor }}</span>
    </div>
    <div class="adjust-item">
      <el-checkbox v-model="pageConfig.originalPriceShow">划线价格</el-checkbox>
      <el-color-picker v-model="pageConfig.originalPriceColor" />
      <span>{{ pageConfig.originalPriceColor }}</span>
    </div>
  </div>

  <div class="divider"></div>
  <div class="func">
    <p class="attr">点击商品</p>
    <div class="adjust-item">
      <el-radio-group v-model="pageConfig.goodsAction">
        <el-radio label="secKillPage">进入活动页</el-radio>
        <el-radio label="goodsDetailPage">进入秒杀详情页</el-radio>
      </el-radio-group>
    </div>
  </div>

  <div class="divider"></div>
  <div class="func">
    <p class="attr">边距</p>
    <div class="adjust-item">
      <span>商品间距</span>
      <el-slider
        v-model="pageConfig.distance"
        :min="0"
        :max="20"
        show-input
        size="small"
      />px
    </div>
    <div class="adjust-item">
      <span>上边距</span>
      <el-slider
        v-model="pageConfig.marginTop"
        :min="0"
        :max="50"
        show-input
        size="small"
      />px
    </div>
    <div class="adjust-item">
      <span>下边距</span>
      <el-slider
        v-model="pageConfig.marginBottom"
        :min="0"
        :max="50"
        show-input
        size="small"
      />px
    </div>
    <div class="adjust-item">
      <span>左右边距</span>
      <el-slider
        v-model="pageConfig.marginHorizontal"
        :min="0"
        :max="20"
        show-input
        size="small"
      />px
    </div>
  </div>
  <div class="divider"></div>
  <div class="func">
    <p class="attr">圆角设置</p>
    <div class="adjust-item">
      <span>上圆角</span>
      <el-slider
        :min="0"
        :max="20"
        v-model="pageConfig.radiusTop"
        show-input
        size="small"
      />px
    </div>
    <div class="adjust-item">
      <span>下圆角</span>
      <el-slider
        :min="0"
        :max="20"
        v-model="pageConfig.radiusBottom"
        show-input
        size="small"
      />px
    </div>
  </div>

  <href-selector
    v-model:show="linkFormVisible"
    :model-value="linkObject"
    :disable-tab="[
      'mallProduct',
      'middlePage',
      'mallPage',
      'systemManage',
      'other',
      'manual',
    ]"
    @confirm="confirmLink"
  />
</template>

<script setup lang="ts">
import type { Ref } from "vue";
import { useHrefSelector } from "@/views/activity/actApp/components/default/compositions/useHrefSelector";
import { ILink } from "@/components/hrefSelector/types";
import storeTypeList from "@/views/activity/actApp/components/default/storeTypeList";
import type { CascaderProps } from "element-plus";
import {
  getActPrizeList,
  getActsByType,
  getActSecKillConfig,
} from "@/api/activity";
import { IActivityByType } from "@/api/modules/activity";
import TitleIcon from "@/assets/app/icon/secKill.png";
import OperationTitle
  from "@/views/activity/actApp/components/default/operationComponents/operationTitle/OperationTitle.vue";

interface IPageConfig {
  custom: string;
  style: string;
  borderColor: string;
  borderWidth: number;
  titleStyle: string;
  title: string;
  titleColor: string;
  titleImg: string;
  moreShow: boolean;
  moreText: string;
  moreColor: string;
  countdownColor: string;
  countdownBg: string;
  goodsSelected: [number, string];
  url: string;
  urlName: string;
  actLink: string;
  canonicalActLink: string;
  componentBgStyle: string;
  componentBgColor: string;
  componentBgUrl: string;
  goodsBgColor: string;
  goodsListBgColor: string;
  goodsNameShow: boolean;
  goodsNameColor: string;
  priceShow: boolean;
  priceColor: string;
  originalPriceShow: boolean;
  originalPriceColor: string;
  goodsAction: string;
  distance: number;
  marginTop: number;
  marginBottom: number;
  marginHorizontal: number;
  radiusTop: number;
  radiusBottom: number;
}

const props = withDefaults(
  defineProps<{
    pageConfig: IPageConfig;
  }>(),
  {
    pageConfig: () => {
      return {
        custom: "1",
        style: "1",
        borderWidth: 1,
        borderColor: "#ededed",
        titleStyle: "text",
        title: "限时秒杀",
        titleColor: "#FFFFFF",
        titleImg: "",
        moreShow: true,
        moreText: "更多",
        moreColor: "#FFFFFF",
        countdownColor: "#FF4D4F",
        countdownBg: "#FFFFFF",
        goodsSelected: [-1, ""],
        componentBgStyle: "color",
        componentBgColor: "#FF4D4F",
        componentBgUrl: "",
        url: "",
        urlName: "",
        actLink: "",
        canonicalActLink: "",
        goodsBgColor: "#FFFFFF",
        goodsListBgColor: "#FFFFFF", //商品组背景色
        goodsNameShow: true,
        goodsNameColor: "#333333",
        priceShow: true,
        priceColor: "#F74B4E",
        originalPriceShow: true,
        originalPriceColor: "#666666",
        goodsAction: "secKillPage",
        distance: 10,
        marginTop: 0,
        marginBottom: 0,
        marginHorizontal: 0,
        radiusTop: 10,
        radiusBottom: 10,
      };
    },
  }
);

const emits = defineEmits<{
  (e: "changeStyle", configStyle: IPageConfig): void;
}>();

// 标题
const titleFileList: Ref<string> = ref<string>("");
const titleFileSuccess = (value: any) => {
  props.pageConfig.titleImg = value.url;
};

// 选择商品
const actPageAreaList: Ref<IActivityByType[]> = ref([]);
const _getActsByType = async () => {
  const rs = await getActsByType({ type: 8 });
  actPageAreaList.value = (rs.payload ?? []).map((item) => {
    return {
      ...item,
      label: item.name,
      value: item.id,
      children: item.pageAreas.map((pItem) => ({
        ...pItem,
        label: pItem.name,
        value: pItem.pageFlag,
      })),
    };
  });
};

const changeGoods = async (value: [number, string]) => {
  // console.log("goods change", value);
  props.pageConfig.goodsSelected = value; // [actId, pageFlag]

  const actConfig = actPageAreaList.value.find((item: {id: number}) => item.id === value[0])
  props.pageConfig.actLink = actConfig?.actLink ?? '';
  props.pageConfig.canonicalActLink = actConfig?.canonicalActLink ?? '';
};

// 关联活动
const { linkFormVisible, linkIndex, linkObject, submitUrl, linkDialog } =
  useHrefSelector();
const confirmLink = (link: ILink) => {
  submitUrl(link, props.pageConfig);
};

// 颜色设置
const componentBgUrlFileList: Ref<string> = ref<string>("");
const componentBgUrlFileSuccess = (value: any) => {
  props.pageConfig.componentBgUrl = value.url;
};

watch(props, () => {
  emits("changeStyle", props.pageConfig);
});

onMounted(() => {
  _getActsByType();
});
</script>

<style lang="scss" scoped>
@use "../styles/operation";

:deep(.icon-arrow-down:before){
  content:''
}

.add-item {
  width: 100%;
  display: block !important;

  .add-item-input {
    background: #f4f3f7;
    border-radius: 4px;
    padding: 10px 20px 0 10px;
    color: #999;
    //   margin-top: 10px;
    //   margin-bottom: 20px;
    position: relative;
    display: flex;
    align-items: center;

    p {
      width: 60px;
    }

    :deep(.el-input) {
      font-size: 12px;
      height: 30px;
      width: calc(100% - 60px);
    }

    &.extra-li {
      justify-content: center;
    }

    .link-div {
      width: calc(100% - 80px);
      display: flex;
      align-items: center;
      background: #e8effc;
      border: 0;
      padding-left: 16px;
      cursor: pointer;
      position: relative;

      :deep(.el-icon) {
        margin-right: 10px;
      }

      :deep(.el-input) {
        font-size: 12px;
        height: 30px;
        width: calc(100% - 30px);
      }

      .close-btn {
        position: absolute;
        right: 5px;
      }
    }

    .sure-close {
      position: absolute;
      right: 5px;
      top: 5px;
      cursor: pointer;
    }
  }

  .add-item-input:last-child {
    padding-bottom: 10px;
  }
}
</style>
